<template>
  <div class="container">
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 表单 -->
      <el-form :model="artForm" inline label-width="80px">
        <!-- 关键字 -->
        <el-form-item label="关键字">
          <el-input
            placeholder="根据文章标题搜索"
            v-model="artForm.keyword"
          ></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="artForm.state" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="clearHandler">清除</el-button>
          <el-button @click="handleFilter" type="primary">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button @click="add" type="success"
            ><i class="el-icon-edit"></i>新增技巧</el-button
          >
        </el-form-item>
      </el-form>

      <!-- 警告 -->
      <template>
        <el-alert
          :title="`数据一共${counts}条`"
          :closable="false"
          type="info"
          show-icon
        >
        </el-alert>
      </template>

      <!-- 表格 -->
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column label="序号" width="80">
            <template v-slot="scope">
              {{ scope.$index + (artForm.page - 1) * artForm.pagesize + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="title" label="文章标题">
            <template v-slot="scope">
              <span>{{ scope.row.title }}</span>
              <el-link
                :underline="false"
                type="primary"
                icon="el-icon-film"
                @click="iconShow(scope.row.videoURL)"
                v-show="scope.row.videoURL"
              ></el-link>
            </template>
          </el-table-column>
          <el-table-column prop="visits" label="阅读数"> </el-table-column>
          <el-table-column prop="username" label="录入人"> </el-table-column>
          <el-table-column prop="createTime" label="录入时间">
            <template slot-scope="scope">
              {{ scope.row.createTime | forMat('YYYY-MM-DD hh:mm:ss') }}
            </template>
          </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="操作">
            <template slot-scope="scope">
              <el-button @click="detailHandler(scope.row)" type="text"
                >预览</el-button
              >
              <el-button type="text" @click="handleStatus(scope.row)">
                {{ scope.row.state ? '禁用' : '启用' }}
              </el-button>
              <el-button
                :disabled="!scope.row.state"
                @click="edit(scope.row)"
                type="text"
                >修改</el-button
              >
              <el-button
                :disabled="!scope.row.state"
                @click="deleteHandler(scope.row)"
                type="text"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!-- 分页 -->
      <template class="el-pagination">
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="formState.page"
            :page-size.sync="formState.pagesize"
            :page-sizes="[2, 5, 10, 20]"
            layout="prev, pager, next, sizes, jumper"
            :total="counts"
          >
          </el-pagination>
        </div>
      </template>

      <!-- 预览的弹出框 -->
      <el-dialog title="预览文章" :visible.sync="dialogVisible" width="55%">
        <articles-dialog :artDetail="artDetail" />
      </el-dialog>

      <!-- 新增文章弹出框 -->
      <el-dialog
        title="新增文章"
        v-if="dialogVisibleAdd"
        :visible.sync="dialogVisibleAdd"
        width="55%"
      >
        <add-dialog
          :is-edit="isEdit"
          :row="row"
          @success="successHandler"
        ></add-dialog>
      </el-dialog>

      <!-- 视频弹出层 -->
      <div v-if="video" class="popContainer" @click="hideVideo">
        <i class="el-icon-circle-close"></i>
        <video
          autoplay="autoplay"
          muted="muted"
          loop="loop"
          controls="controls"
          :src="videoURl"
          class="video"
        ></video>
      </div>
    </el-card>
  </div>
</template>

<script>
// import { parseTime } from '../../filters/index'
import { list, changeState, detail, remove } from '@/api/hmmm/articles'
// import { status } from '@/api/hmmm/constants'
import articlesDialog from './articlesDialog'
import addDialog from './addDialog'
export default {
  components: {
    'articles-dialog': articlesDialog,
    'add-dialog': addDialog
  },
  data () {
    return {
      artForm: {
        page: 1,
        pagesize: 5,
        keyword: null,
        state: null
      },
      formState: {
        page: 1,
        pagesize: 5
      },
      tableData: [],
      counts: 1,
      dialogVisible: false,
      artDetail: {},
      // 新增文章
      dialogVisibleAdd: false,
      isEdit: false,
      row: {},
      // 视频
      videoURl: '',
      video: false,
      dialogVisibleVideo: false
    }
  },
  created () {
    // 获取文章列表
    this.articleList()
    this.reset()
  },

  methods: {
    reset () {
      this.artForm = {
        page: 1,
        pagesize: 5,
        keyword: null,
        state: null
      }
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.artForm.pagesize = val
      this.articleList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.artForm.page = val
      this.articleList()
    },
    // 获取文章列表
    async articleList () {
      const res = await list(this.formState)
      this.tableData = res.data.items
      this.counts = res.data.counts
    },
    // 启用、禁用
    async handleStatus (val) {
      var states = {
        id: val.id,
        state: val.state ? 0 : 1
      }
      const res = await changeState(states)
      if (!res.data.success) return
      const index = this.tableData.findIndex((item) => item.id === val.id)
      this.tableData[index].state = val.state ? 0 : 1
      this.$message.success('操作成功')
    },
    // 搜索
    async handleFilter () {
      // 搜索需要带状态
      this.formState = this.artForm
      this.articleList()
    },
    // 点击预览
    async detailHandler (row) {
      this.dialogVisible = true
      const res = await detail({ id: row.id })
      this.artDetail = res.data
    },
    // 新增技巧
    add () {
      this.dialogVisibleAdd = true
      this.isEdit = false
    },
    // 新增文章成功的回调
    successHandler () {
      this.dialogVisibleAdd = false
      // 刷新列表
      this.articleList()
    },
    // 点击编辑按钮
    edit (row) {
      this.dialogVisibleAdd = true
      this.isEdit = true
      this.row = row
    },
    // 删除
    deleteHandler (row) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求删除数据
          await remove(row)
          if (this.tableData.length === 1 && this.artForm.page > 1) {
            this.artForm.page--
          }
          this.articleList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 清除
    clearHandler () {
      this.reset()
    },
    // 视频
    iconShow (url) {
      this.video = true
      // console.log(url, 78979)
      this.videoURl = url
    },
    // 隐藏弹出层
    hideVideo () {
      this.video = false
    }
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 10px;
  .el-pagination {
    // float: right;
    margin-top: 20px;
  }
  .el-pagination {
    text-align: right;
  }
  .popContainer {
    z-index: 999;
    display: flex;
    justify-content: space-around;
    flex-direction: column;
    align-items: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 800px;
    height: 600px;
    .el-icon-circle-close {
      font-size: 40px;
      opacity: 0.5;
    }
    .video {
      width: 100%;
    }
  }
  ::v-deep .el-icon-film {
    color: blue;
    font-size: 18px;
  }
  video::-webkit-media-controls-fullscreen-button {
    display: none;
  }
  //播放按钮
  video::-webkit-media-controls-play-button {
    display: none;
  }
  //进度条
  video::-webkit-media-controls-timeline {
    display: none;
  }
  //观看的当前时间
  video::-webkit-media-controls-current-time-display {
    display: none;
  }
  //剩余时间
  video::-webkit-media-controls-time-remaining-display {
    display: none;
  }
  //音量按钮
  video::-webkit-media-controls-mute-button {
    display: none;
  }
  video::-webkit-media-controls-toggle-closed-captions-button {
    display: none;
  }
  //音量的控制条
  video::-webkit-media-controls-volume-slider {
    display: none;
  }
  //所有控件
  video::-webkit-media-controls-enclosure {
    display: none;
  }
}
</style>
